function flag_success=copy2H(outfold)
% =========================================================================
% COPY2H(outfold); 
% 
% When copying output to folder OUTFOLD which is not in DRIVE H --e.g. when
% running matlab on C, my documents or other local drive-- this m-file will
% make a copy of OUTFOLD to the same path but in the H drive. 
% Exceptions: if running from UNIX or a machine that cannot match to H,
% will not even try. Output FLAG_SUCCESS will be 0. If called from H,
% copying not needed and output FLAG_SUCCESS will be 1. 
% Otherwise, it first searches for a set of roots such as ESTDSGE,FTVAR,LMJ,etc. which
% are the main target folders in H, and then copies to a folder in that root path. 
% If it fails on first attempt, it will wait for 10 second and then repeat.
% Success at any of the two attemps yield FLAG_SUCCESS=1. 
% If that fails as well then it will try to copy the path to H:\COPIES. 
% Success at this step yields FLAG_SUCCESS=0.5. 
% If there is no success after this third attempt, code prints a warning message but DOES NOT CRASH. 
% This m-file should be used only once all operations are completed, to
% avoid disrupting the code.
% 
% If FLAG_SUCCESS=1, then it will write a TXT file RUN_SOURCE, indicating
% the PATH where the original output is stored. 
%
% If called from within H: this function is ignored and returns.
% but FLAG_SUCCESS is still 1. 
%
% Alejandro Justiniano April 21 2009 
% =========================================================================
cucd=pwd; 
flag_success=0; 
if strcmp('H:\',outfold(1:3) )==1 
    disp('Running from H, copy not needed'); 
    flag_success=1; 
    return 
end 
if isunix==1 
    disp('Running from UNIX, no copy to H: possible'); 
    return 
end 
% Try to connect to H drive 
try 
    cd('H:\'); 
catch 
    disp('H: path does not exist') 
    lasterr 
    return 
end 
cd(cucd); 
roots={'estdsge','lmj','ftvar','cej','sur'}; 
for ii=1:length(roots); 
    startmatch=strfind(lower(outfold),lower(roots{ii})); 
    if ~isempty(startmatch);
        break; 
    end 
end 
if isempty(startmatch);
    dispaj('Cannot find any root in ',outfold); 
    error('Cannot find root in OUTFOLD')
end 
path=['H:\',outfold(startmatch:end)];
[success,message]=copyfile(outfold,path);
if success==1
    dispaj('Successfully copied to ',path);
    flag_success=1; 
    writeroot; 
    return;
elseif success==0;
    dispaj('Cannot copy to ',path);
    disp(message); 
    pause(10);
    % 2nd try
    [success2,message]=copyfile(outfold,path);
    if success2==0;
        disp('2nd attempt failed');
        disp(message);
        % Last attempt H:\copies is a temporary repository
        path=cr_dir('H:\copies',outfold(startmatch:end));
        [success3,message]=copyfile(outfold,path);
        if success3==0;
            disp('Cannot copy even to H:\copies');
            disp(message);
            warning('Cannot copy even to H:\copies');
        else 
            dispaj('Successfully copied to ',path);
            flag_success=0.5;
            return;
        end 
    else
        dispaj('Successfully copied to ',path);
        flag_success=1;
        writeroot; 
        return; 
    end
end
    function writeroot
        try
            cd(path);
            fid=fopen('RUN_SOURCE.txt','wt');
            fprintf(fid,'Path where run=%s\n',outfold);
            fclose(fid);
            cd(cucd);
        catch
            disp('Could not write RUN_SOURCE.txt')
            lasterr
        end
    end
end


